package com.wanmait.baohan.controller.logisticsManagement;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.*;
import com.wanmait.baohan.vo.CarDataVo;
import com.wanmait.baohan.vo.DataVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/management")
public class TicketManagementController {

    @Resource
    private BookingTicketApplicationService bookingTicketApplicationService;
    @Resource
    private BookingCountService bookingCountService;
    @Resource
    private HotelAccountService hotelAccountService;
    @Resource
    private ProvincesCityService provincesCityService;
    @Resource
    private VehicleService vehicleService;
    @Resource
    private VehicleAccountService vehicleAccountService;
    @Resource
    private TravelApplicationService travelApplicationService;
    @Resource
    private TravelPeopleService travelPeopleService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private TicketApprovalService ticketApprovalService;
    @Resource
    private VehicleReservationService vehicleReservationService;
    @Resource
    private BookingCarApplicationService bookingCarApplicationService;
    @Resource
    private BookingHotelApplicationService bookingHotelApplicationService;
    //跳转到订票台账页面
    @RequestMapping(value = "/booking")
    public String booking(Model model,String applicationId){
        model.addAttribute("applicationId",applicationId);
        System.out.println(applicationId);
        List<TravelPeople> travelPeoples=travelPeopleService.findAllAllpicationStaffs(Integer.parseInt(applicationId));
        for (TravelPeople travelPeople:travelPeoples) {
            System.out.println("名字"+travelPeople.getStaff().getName());
        }
        model.addAttribute("travelPeoples",travelPeoples);
        return "/management/booking";
    }
    //跳转到订车台账页面
    //不是getcar
    @RequestMapping(value ="/car")
    public String car(Model model,String startTime,String endTime,String startLocation,String endLocation,Integer bookingCarApplicationId){
//        System.out.println("bookingCarApplicationId===="+bookingCarApplicationId);
//        System.out.println("startLocation======"+startLocation);
//        System.out.println("endLocation====="+endLocation);
        model.addAttribute("startTime",startTime);
        model.addAttribute("startLocation",startLocation);
        model.addAttribute("endTime",endTime);
        model.addAttribute("endLocation",endLocation);
        model.addAttribute("bookingCarApplicationId",bookingCarApplicationId);
        return "management/car";
    }
    //跳转到订酒店台账页面
    @RequestMapping(value = "/hotel")
    public String hotel(Model model,Integer applicationId){
        List<ProvincesCity> proviceCities=provincesCityService.findAllCity();
        model.addAttribute("proviceCities",proviceCities);
        model.addAttribute("applicationId",applicationId);
        //System.out.println(applicationId);
        return "management/hotel";
    }
    //订车机票台账提交
    @RequestMapping("/submitbooking")
    public String booking(BookingAccount bookingAccount, HttpServletRequest request) {

        Staff staff = (Staff)request.getSession().getAttribute("user");
        if (staff!=null)
        {
            bookingAccount.setAddStaffId(staff.getId());
        }else {
            bookingAccount.setAddStaffId(1);
        }
        //System.out.println(bookingAccount.toString());
        bookingCountService.insert(bookingAccount);

        return "/management/allTicketApplicationsList";
    }
    //酒店台账提交
    @RequestMapping("/submithotel")
    public String submithotel(HotelAccount hotelAccount, HttpServletRequest request,String applicationId) {
        System.out.println("酒店");
        System.out.println(applicationId);
        Staff staff = (Staff)request.getSession().getAttribute("user");
        if (staff!=null)
        {
            hotelAccount.setAddStaffId(staff.getId());
        }else {
            hotelAccount.setAddStaffId(1);
        }
        //System.out.println(hotelAccount.toString());
        BookingHotelApplication bookingHotelApplication =new BookingHotelApplication();
        bookingHotelApplication.setId(Integer.parseInt(applicationId));
        hotelAccount.setBookingHotelApplication(bookingHotelApplication);
        hotelAccountService.insert(hotelAccount);
        bookingHotelApplicationService.updateStateById(Integer.parseInt(applicationId),3);
        return "/management/allHotelApplicationsList";
    }
    //获得所有关于车辆的信息
    @RequestMapping("getCar")
    @ResponseBody
        public DataVo<Vehicle> getCar(String page, String limit, String startTime,String endTime){
        //System.out.println("获取车辆" + page + limit + startTime + endTime);
            //2020-12-15 16:21:40
            //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            //根据有无时间查出不同的结果
//            List<Vehicle> vehicles=vehicleService.findLeisureCar("".equals(startTime)?null:startTime,"".equals(endTime)?null:endTime);
            List<Vehicle> vehicles=vehicleService.findLeisureCar(startTime, endTime);
            DataVo dataVo = new DataVo();
            dataVo.setCount((long) vehicles.size());
            dataVo.setData(vehicles);
            return dataVo;
    }
    //获得派车申请表
    //派车
    @RequestMapping("setCar")
    @ResponseBody
    public void setCar(String carData,String startTime,String endTime,Integer bookingCarApplicationId,String startLocation,String endLocation){
        System.out.println("开始派车");
        List<CarDataVo> cars = JSON.parseObject(carData,new TypeReference<List<CarDataVo>>(){});
        List<VehicleReservation> vehicleReservations = new ArrayList<>();
        for (CarDataVo car : cars) {
            //System.out.println(car.getId());
            VehicleReservation vehicleReservation = new VehicleReservation();
            vehicleReservation.setStartTime(startTime);
            vehicleReservation.setEndTime(endTime);
            vehicleReservation.setVehicleId(car.getId());
            vehicleReservation.setEndLocation(endLocation);
            vehicleReservation.setStartLocation(startLocation);
            vehicleReservation.setBookingCarApplicationId(bookingCarApplicationId);
            vehicleReservations.add(vehicleReservation);
        }
        //车辆预约表里新增多条数据
        vehicleReservationService.insertList(vehicleReservations);
        //把 派车申请表 state状态改为：3处理完成
        bookingCarApplicationService.findByIdUpdateState(bookingCarApplicationId);
    }
    //提交派车台账
    @RequestMapping("/submitCar")
    public String submitCar(VehicleAccount vehicleAccount, HttpServletRequest request) {
        System.out.println(111);
        Staff staff = (Staff)request.getSession().getAttribute("user");
        if (staff!=null)
        {
            vehicleAccount.setAddStaffId(staff.getId());
        }else {
            vehicleAccount.setAddStaffId(1);
        }
        //从session里取出差申请表id，这里先给假数据
        vehicleAccount.setTravelApplicationId(1);
        vehicleAccountService.update(vehicleAccount);
        return "/index";
    }




    /**
     * hxn
     * @return
     * 打开所有可订票的项目列表页面
     */
    @RequestMapping("/applicationsCanBuyTicket")
    public String applicationsCanBuyTicket(){
        return "management/applicationsCanBuyTicket";
    }

    /**
     * hxn
     * @param request
     * @return
     * 获取所有可以订票的项目
     */
    @RequestMapping("/getApplicationsCanBuyTicket")
    @ResponseBody
    public DataVo<TravelApplication> getApplicationsCanBookCar(HttpServletRequest request){
        DataVo<TravelApplication> dataVo = new DataVo<>();
        List applications = null;
        Staff staff = (Staff) request.getSession().getAttribute("user");
        Integer userId = staff.getId();
        applications = travelApplicationService.findAllApplicationCanBooking(userId);
        dataVo.setData(applications);
        dataVo.setCount((long) applications.size());
        return dataVo;
    }

    /**
     * 打开我的所有订票申请
     * @param model
     * @return
     */
    @RequestMapping("table-TicketApplications")
    public String tebleTicketApplciations(Model model){
        return "management/table-TicketApplications";
    }

    /**
     * hxn
     * @param model
     * @return
     * 订票申请页面
     */
    @RequestMapping("/bookingTicketApplication")
    public String bookingTicketApplication(Integer id, Model model){
        //model.addAttribute("projectTypes", dictionaryService.getBookingTicketProjectType());
        model.addAttribute("allCity", provincesCityService.findAllCity());
        model.addAttribute("transportType", dictionaryService.getTransportType());
        model.addAttribute("applicationBasicInfo", travelApplicationService.findById(id));
        model.addAttribute("applicationMembers",travelPeopleService.findAllAllpicationStaffs(id));
        model.addAttribute("airplaneSeat", dictionaryService.getAirplaneSeat());
        model.addAttribute("trainSeat", dictionaryService.getTrainSeat());
        model.addAttribute("highspeedTrainSeat",dictionaryService.getHighSpeedTrainSeat());
        model.addAttribute("busSeat",dictionaryService.getBusSeat());
        return "/management/bookingTicketApplication";
    }

    /**
     * hxn
     * @param bookingTicketApplication
     * 提交订票申请
     */
    @RequestMapping("/submitTicketApplication")
    @ResponseBody
    public void submitTicketApplication(BookingTicketApplication bookingTicketApplication){
        //System.out.println(bookingTicketApplication.toString());
        bookingTicketApplicationService.submitTicketApplication(bookingTicketApplication);
    }

    /**
     * hxn
     * @param page
     * @param limit
     * @param request
     * @return
     * 获取我的申请
     */
    @RequestMapping("/getMyTicketApplications")
    @ResponseBody
    public DataVo<BookingTicketApplication> getMyTicketApplications(Integer page, Integer limit, HttpServletRequest request){
        BookingTicketApplication bookingTicketApplication = new BookingTicketApplication();
        Staff staff = (Staff) request.getSession().getAttribute("user");
        bookingTicketApplication.setStaff(staff);

        DataVo<BookingTicketApplication> dataVo = new DataVo<>();
        PageInfo<BookingTicketApplication> pageInfo = bookingTicketApplicationService.selectMyTicketApplications(page, limit, bookingTicketApplication);
        dataVo.setCount(pageInfo.getTotal());
        dataVo.setData(pageInfo.getList());
        return dataVo;
    }

    /**
     * hxn
     * @param id
     * @param model
     * @return
     * 获取该订票申请详情
     */
    @RequestMapping("/getTicketApplicationDetails")
    public String getCarApplicationDetails(Integer id, Model model){
        BookingTicketApplication bookingTicketApplication = bookingTicketApplicationService.findById(id);
        model.addAttribute("bookingTicketApplication", bookingTicketApplication);
        model.addAttribute("approvalLog",ticketApprovalService.findById(id));
        return "management/ticketApplicationDetails";
    }

    @RequestMapping("allTicketApplicationsList")
    public void allTicketApplicationsList(){

    }

    /**
     * hxn
     * @param page
     * @param limit
     * @param bookingTicketApplication
     * @return
     * 获取所有的订票申请，条件查询
     */
    @RequestMapping("getAllTicketApplications")
    @ResponseBody
    public DataVo<BookingTicketApplication> getAllTicketApplications(Integer page, Integer limit, BookingTicketApplication bookingTicketApplication){
        DataVo<BookingTicketApplication> dataVo = new DataVo<>();
        PageInfo<BookingTicketApplication> pageInfo = bookingTicketApplicationService.findAllWithPageAndLimit(page,limit,bookingTicketApplication);
        dataVo.setData(pageInfo.getList());
        dataVo.setCount(pageInfo.getTotal());
        return dataVo;
    }

    /**
     * hxn
     * @param id
     * @param value
     * @param state
     * 保存审核结果
     */
    @RequestMapping("saveTicketApprovalForm")
    @ResponseBody
    public void saveTicketApprovalForm(Integer id, String value, Integer state){
        TicketApproval ticketApproval = new TicketApproval();
        ticketApproval.setApprovalInfo(value);
        ticketApproval.setBookingTicketApplicationId(id);
        ticketApproval.setState(state);
        ticketApprovalService.submitApprove(ticketApproval);
    }

    /**
     * 王森明
     * 打开查看订票台账的iframe
     */
    @RequestMapping("ticketAccount")
    public  void ticketAccount(Integer id,Model model){
      List<BookingAccount> lsit = bookingCountService.findBookingListById(id);
      model.addAttribute("list",lsit);
    }
}
